package com.hmdp.utils;

import cn.hutool.core.bean.BeanUtil;
import com.hmdp.dto.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.hmdp.utils.SystemConstants.TOKEN_EXPIRE;
import static com.hmdp.utils.SystemConstants.USER_TOKEN_PREFIX;

@Slf4j
public class RefreshInterceptor implements HandlerInterceptor,ApplicationContextAware {
//    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //每个请求都更新有效期
        String token = request.getHeader("authorization");
            stringRedisTemplate.expire(USER_TOKEN_PREFIX + token,TOKEN_EXPIRE, TimeUnit.MINUTES);
            log.debug("更新用户信息有效期");
        return true;
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        stringRedisTemplate=applicationContext.getBean(StringRedisTemplate.class);
    }
}
